//
//  TherapyDetailViewController.m
//  Happy Kitchen
//
//  Created by mac_chen on 15/3/7.
//  Copyright (c) 2015年 mac_chen. All rights reserved.
//

#import "TherapyDetailViewController.h"

@interface TherapyDetailViewController ()

@end

@implementation TherapyDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = self.officeName;
    
    //自定义tableVIew
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49 - 64)style:UITableViewStylePlain];
    //签协议
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    
    //菊花类
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.HUD];
    self.HUD.labelText = @"加载中";
    [self.HUD showAnimated:YES whileExecutingBlock:^{
   //自定义方法
        [self doTask];
        
    } completionBlock:^{
       
        
    }];
    
    
    //初始化字典,数组
    self.myArray = [NSMutableArray array];
    self.myDic = [NSMutableDictionary dictionary];
    //AFN解析数据
    //字符串拼接
    NSString *str1 = @"http://42.121.13.106:8080/HandheldKitchen/api/vegetable/tbldisease!getDisease.do?&is_traditional=0&";
    NSString *str2 = [NSString stringWithFormat:@"officeId=%@", self.officeId];
    NSString *str = [str1 stringByAppendingString:str2];
    //调用AFN
    [GetAFN downLoadBlock:str block:^(id result) {
        self.myDic = result;
        NSMutableArray *array = [NSMutableArray arrayWithArray:[self.myDic objectForKey:@"data"]];
        for (NSDictionary *dic in array) {
            //初始化
            TherapyDetail *therapyDetail = [[TherapyDetail alloc] init];
            //KVC赋值
            [therapyDetail setValuesForKeysWithDictionary:dic];
            //把model添加到数组中
            [self.myArray addObject:therapyDetail];
        }
        //刷新tableVIew
        [self.myTableView reloadData];
        //刷新完成,结束菊花类
         [self.HUD removeFromSuperview];
    }];
   
    
}

//自定义方法,设置菊花存在的时间
- (void)doTask
{
    sleep(20);
}

//返回row的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.myArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    TherapyDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[TherapyDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    //初始化
    TherapyDetail *therapyDetail = [self.myArray objectAtIndex:indexPath.row];
    
    //sd异步请求数据
    NSString *urlStr = therapyDetail.imageName;
    NSString *urlStrEncode = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:urlStrEncode];
    [cell.imageNameImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"loading.jpg"]];
    
    [cell setTherapyDetail:therapyDetail];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

//tableView点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //cell点击事件,push到菜单详情页面
    MenuListViewController *menuListlVC = [[MenuListViewController alloc] init];
    
    TherapyDetail *therapyDetail = [self.myArray objectAtIndex:indexPath.row];
    //给controller传值
    
    menuListlVC.therapyDetail = therapyDetail;
    menuListlVC.diseaseId = therapyDetail.diseaseId;
    menuListlVC.diseaseDescribe = therapyDetail.diseaseDescribe;
    menuListlVC.fitEat = therapyDetail.fitEat;
    menuListlVC.lifeSuit = therapyDetail.lifeSuit;
    //push到列表页面
   [self.navigationController pushViewController:menuListlVC animated:YES];
}

//设置row的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
